#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MSG_SZ 64
#define PATH_NAME "."
#define PROJ_ID 2
#define MSG_TYPE 101



struct msgbuf {
    long msgtype;
    char msgtext[MSG_SZ];
};

int main(int argc, char const *argv[])
{
    /* code */
    struct msgbuf msg;

    int msgId;
    
    key = ftok(PATH_NAME, PROJ_ID);
    if (key == -1) {
        perror("ftok error ");
        exit(EXIT_FAILURE);
    }

    msgId = msgget(key, 0666 | IPC_CREAT);
    printf("msgId: %d\n", msgId);
    
    ssize_t rbytes = msgrcv(msgId, (void *)&msg, MSG_SZ, MSG_TYPE, 0);
    printf("Received %ld bytes\n", rbytes);
    if (rbytes == -1) {
        perror("msgrcv error ");
        exit(EXIT_FAILURE);
    }

    printf("Received message: %s\n", msg.msgtext);
    return 0;
}
